home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / music / cthugha5.zip / CTHU5SRC.ZIP / CTHUGHA.C < prev    next >
C/C++ Source or Header  |  1994-08-19  |  26KB  |  1,067 lines

  1. //
  2. // Cthugha - Audio Seeded Image Processing
  3. //
  4. // Zaph, Digital Aasvogel Group, Torps Productions 1993-1994
  5. //
  6. // Copyright Information:
  7. // ======================
  8. // 
  9. // Some parts of the source are from the public domain and are not
  10. // copyrighted.
  11. // 
  12. // Some parts of the source bear explicit copyright notices from the
  13. // author and are subject to the conditions listed there by the author.
  14. // 
  15. // The remainder of the source (not already public domain, no explicit
  16. // author's copyright notice) is Copyright 1994 by Torps Productions
  17. // (a loosely associated and ever-growing group of fanatic programmers).
  18. //
  19. // The source code may be copied freely and may be used in other programs
  20. // under the following conditions:
  21. //  It may not be used in a commercial program without prior permission.
  22. //  Please credit the author (in general, credit Cthugha and Torps
  23. //  Productions) as the source of the code.
  24. // 
  25. // This copyright notice is grafted from the Fractint copyright notice.
  26. //
  27. //
  28. // Modifying the code:
  29. // ===================
  30. //
  31. // Feel free to modify this source code, HOWEVER, please send any working
  32. // changes/fixes to me (zaph@torps.apana.org.au) for inclusion in future
  33. // versions of the code.
  34. //
  35. // Distributing the code:
  36. // ======================
  37. //
  38. // Feel free to distribute this code, as long as it is complete and contains
  39. // all copyright information that was included in the original.
  40. //
  41. // Legal Issues:
  42. // =============
  43. //
  44. // What legal issues ???
  45. //
  46. // Come on guys, this is for *fun*, get on with it, make it great, make us
  47. // all famous!!!
  48. //
  49. //                                                zaph, 12May94
  50.  
  51.  
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <dos.h>
  55. #include <io.h>
  56. #include <fcntl.h>
  57. #include <math.h>
  58. #include <conio.h>
  59. #include <time.h>
  60. #include <sys\types.h>
  61. #include <sys\stat.h> 
  62. #include <string.h>
  63. #include <memory.h>
  64.  
  65.  
  66. #include "sb.h"
  67. #include "cthugha.h"
  68. #include "charset.h"
  69. #include "zorilkey.h"
  70. #include "initscrn.h"
  71. #include "maps.h"
  72. #include "translat.h"
  73.  
  74. #include "audio.h"
  75.  
  76. #include "mouse.h"
  77.  
  78. int useinifile=0;
  79. int massageStyle=0;
  80. int minnoise=4;
  81. int was_quiet=0;
  82. int time_to_change=0;
  83. int allow_fft=1,use_fft=0;
  84.  
  85. extern int peaklevel;
  86. extern int peakframes;
  87.  
  88. extern char stringtable[20][21];
  89. extern int newwave,usewave;
  90. #define FUNC1 03
  91.  
  92. void FillLUTBuffer(int pal);
  93. void WriteBuff(unsigned char *buff);
  94.  
  95. unsigned char LUTbuffer[LUTSIZE];
  96.  
  97. int table[NUMTABLES][256];
  98. int curtable=0;
  99. int stereo[BUFF_WIDTH][2];
  100. int curflame=0;
  101. int curdisplay=0;
  102. int locked=0;
  103. int debug_mode;
  104.  
  105. int curpal=0;
  106. unsigned char buff[BUFF_HEIGHT][BUFF_WIDTH];
  107. extern void fill_buff(void);
  108. extern void flame_cro(void);
  109. extern int optind;    /* index of which argument is next      */
  110. extern char *optarg;  /* pointer to argument of current option */
  111. extern int opterr;    /* allow error message  */
  112. extern int getopt(int argc, char *argv[], char *optionS);
  113.  
  114. static int LoadLuts( char * fn, unsigned char *pal);
  115.  
  116. int in_vol=-1;
  117. int min_time=200;
  118. int rand_time=750;
  119. int quiet_change=1;
  120. extern void draw_text(int xpos, int ypos, int size, int colour, char *tbuf);
  121. extern void init_pete(void);
  122.  
  123. unsigned int tempBuff;
  124. long buffer_size=0x20000L;
  125. char far *voice_buffer;
  126. unsigned  int voice_seg=0;
  127. int debug_mode=0;
  128. unsigned int sample_rate=44100;
  129.  
  130. extern unsigned vu_rate;
  131.  
  132. int mouse_inst;
  133.  
  134. #define UNKNOWN -1
  135. #define SBPRO 0
  136. #define GUS 1
  137. #define OLDSB 2
  138.  
  139. extern char maptabfile[255];
  140. char stringfile[255];
  141. char inifile[255];
  142. char string[255];
  143.  
  144. typedef struct {
  145.     int min_time;
  146.     int rand_time;
  147.     int quiet_change;
  148.     int curtable;
  149.     int curflame;
  150.     int curdisplay;
  151.     int locked;
  152.     int minnoise;
  153.     int massageStyle;
  154.     int allow_fft;
  155.     int use_fft;
  156.     int peaklevel;
  157.     int peakframes;
  158.     int newwave;
  159.     int usewave;
  160.     int extra;
  161.     int curpal;
  162.     unsigned int sample_rate;
  163.     int sample_stereo;
  164.     int translate;
  165. } INIsettings;
  166.  
  167. #define MAX_INI_SETTINGS 50
  168.  
  169. INIsettings personalINI[MAX_INI_SETTINGS];
  170.  
  171. int sound_card=UNKNOWN;
  172.  
  173. int pause=0;
  174.  
  175. int main(int argc, char *argv[])
  176. {
  177.     union REGS regset;
  178.     int     i,j,n;
  179.     int doit=0;
  180.     int temp,error=0,help=0;
  181.     int ch;
  182.     int finished;
  183.     long l;
  184.     int iniline=0;
  185.     int current=0,lastmode=0;
  186.     struct find_t dos_f;
  187.     int use_internal_palettes=TRUE;
  188.     int use_external_palettes=TRUE;
  189.     FILE *fp;
  190.  
  191.     printf("\n\nCTHUGHA v5.0g  Compiled on %s %s\n\n",__DATE__,__TIME__);
  192.     printf("Coded by - The Digital Aasvogel Group - 1994\n\n");
  193.     printf("Original Idea & Code:          Kevin Burfitt (zaph@torps.apana.org.au)\n");
  194.     printf("Flames/Waves/Rotations/SB/FFT: Kevin Burfitt (zaph@torps.apana.org.au)\n");
  195.     printf("SB-Pro(stereo)/GUS/CD player:  Daniel Sachs  (u23783@uic.edu)\n");
  196.     printf("GUS support:                   Jochen Quante (ukr8@rz.uni-karlsruhe.de)\n\n");
  197.  
  198.     delay(1);
  199.  
  200.     sound_card = UNKNOWN;
  201.  
  202.     if( getenv("ULTRASND") )
  203.          sound_card = GUS;
  204.  
  205.     time(&l);
  206.     srand(l);
  207.  
  208.     sprintf(inifile,"CTHUGHA.INI");
  209.  
  210.     newwave=rand()%NUMWAVES;
  211.     curflame=rand();
  212.     stringfile[0]=0;
  213.  
  214.     while((ch = getopt(argc,argv,"?OGPXxLlMmCeidSsT:aQ:V:v:R:f:w:p:t:q:b:B:I:")) != EOF) {
  215.         switch(ch) {
  216.             case 'B':
  217.                 peaklevel = atoi(optarg);
  218.                 printf("Setting peak level to %d\n",peaklevel);
  219.                 break;
  220.             case 'b':
  221.                 peakframes = atoi(optarg);
  222.                 printf("Setting # of peak frames to %d\n",peakframes);
  223.                 break;
  224.             case 'q':
  225.                 strcpy(stringfile,optarg);
  226.                 printf("Reading Strings from %s\n",stringfile);
  227.                 break;
  228.             case 'I':
  229.                 strcpy(stringfile,optarg);
  230.                 printf("Using INI file: %s\n",inifile);
  231.                 useinifile=1;
  232.                 break;
  233.             case 't':
  234.                 strcpy(maptabfile,optarg);
  235.                 printf("Using mapping table file:%s\n",maptabfile);
  236.                 break;
  237.             case 'P':
  238.                 printf("PAS not currently supported - sorry :-(\n");
  239.                 printf("Trying OLD-SB code.\n");
  240.                 sound_card=OLDSB;
  241.  
  242.                 break;
  243.             case 'd':
  244.                 pause=1;
  245.                 break;
  246.             case 'Q':
  247.                 quiet_change=atoi(optarg);
  248.                 if (quiet_change<0)
  249.                     quiet_change=0;
  250.  
  251.                 if (quiet_change)
  252.                     printf("Setting auto-change on silence to %d frames\n",quiet_change);
  253.                 else
  254.                     printf("Disabling auto-change on silence\n");
  255.  
  256.                 break;
  257.             case 'L':
  258.                 device=LineInput;
  259.                 printf("Using Line input\n");
  260.                 break;
  261.             case 'v':
  262.                 vu_rate = (unsigned)(atol(optarg));
  263.  
  264.                 if (vu_rate<4000)
  265.                     vu_rate=0;
  266.  
  267.                 if( vu_rate )
  268.                     printf("Setting VU-bar sample rate to: %u\n",vu_rate);
  269.                 else
  270.                     printf("Setting VU-bar sample rate to current rate\n");
  271.  
  272.                 break;
  273.             case 'V':
  274.                 in_vol = atoi(optarg);
  275.                 printf("Setting input volume to: %d\n",in_vol);
  276.                 break;
  277.             case 'e':
  278.                 use_external_palettes=FALSE;
  279.                 printf("Not Using EXTERNAL palettes\n");
  280.                 break;
  281.             case 'i':
  282.                 use_internal_palettes=FALSE;
  283.                 printf("Not Using INTERNAL palettes\n");
  284.                 break;
  285.             case 'M':
  286.                 device=MicInput;
  287.                 printf("Using Mic input\n");
  288.                 break;
  289.             case 'C':
  290.                 printf("Using CD input\n");
  291.                 device=CDInput;
  292.                 break;
  293.             case 'T':
  294.                 min_time = atoi(optarg);
  295.                 min_time = min(min_time,32000);
  296.                 min_time = max(min_time,0);
  297.                 printf("Minimum frames set to:%d\n",min_time);
  298.                 break;
  299.             case 'R':
  300.                 rand_time = atoi(optarg);
  301.                 rand_time = min(rand_time,32000);
  302.                 rand_time = max(rand_time,1);
  303.                 printf("random extra frames set to:%d\n",rand_time);
  304.                 break;
  305.  
  306.             case 'G':
  307.                 sound_card=GUS;
  308.                 break;
  309.             case 'S':
  310.                 sound_card=SBPRO;
  311.                 break;
  312.             case 'O':
  313.                 sound_card=OLDSB;
  314.                 break;
  315.             case 's':
  316.                 sample_stereo=1;
  317.                 printf("Mode set to STEREO\n");
  318.                 break;
  319.             case 'm':
  320.                 sample_stereo=0;
  321.                 printf("Mode set to MONO\n");
  322.                 break;
  323.             default:
  324.                 printf("Unknown/bad option -%c\n",ch);
  325.                 error++;
  326.                 break;
  327.             case '?':
  328.                 help++;
  329.                 break;
  330.             case 'x':
  331.             case 'X':
  332.                 debug_mode++;
  333.                 printf("Debug Mode (Soundcard not used) (level %d)\n",debug_mode);
  334.                 break;
  335.             case 'f':
  336.                 curflame = atoi(optarg);
  337.                 if (curflame<0)
  338.                     curflame=0;
  339.                 printf("Starting with flame number %d\n",curflame);
  340.                 break;
  341.             case 'p':
  342.                 curpal = atoi(optarg);
  343.                 if (curpal<0)
  344.                     curpal=0;
  345.                 printf("Starting with palette number %d\n",curpal);
  346.                 break;
  347.             case 'w':
  348.                 newwave = atoi(optarg)%NUMWAVES;
  349.                 if (newwave<0)
  350.                     newwave=0;
  351.                 printf("Starting with wave number %d\n",newwave);
  352.                 break;
  353.             case 'l':
  354.                 locked=1;
  355.                 printf("Starting in LOCKED mode\n");
  356.                 break;
  357.         }
  358.      }
  359.  
  360.      if( (error)||(help) ) {
  361.         printf("Cthugha: Command line options  ");
  362.         printf("(These are Case sensitive commands)\n");
  363.         printf("  -L,-M,-C  : Use Line/Mic/CD as input\n");
  364.         printf("  -G,-S,-P  : Use GUS/SB/PAS (default: search)\n");
  365.         printf("  -O        : Use Old SB code (for incompatible cards)\n");
  366.         printf("  -s,-m     : Default to STEREO/MONO\n");
  367.         printf("  -i,-e     : Disable Inbuilt/External Palettes\n");
  368.         printf("  -T <num>  : Minimum frames before changing\n");
  369.         printf("  -R <num>  : extra random frames before changing\n");
  370.         printf("  -V <0-255>: Set input volume to <0-255>\n");
  371.         printf("  -v <rate> : VU bar sample rate (Default=4000)\n");
  372.         printf("  -Q <num>  : Number of frames before silence change <def:200>\n");
  373.         printf("  -f <num>  : Start with flame   #<num>\n");
  374.         printf("  -w <num>  : Start with wave    #<num>\n");
  375.         printf("  -p <num>  : Start with palette #<num>\n");
  376.         printf("  -l        : Lock to first wave/flame/palette\n\n");
  377.         printf("  -d        : Pause just before running (usually for debug)\n");
  378.         printf("  -t <fname>: Load alternate Translation table\n");
  379.         printf("  -q <fname>: Load alternate Quiet messages\n");
  380.         printf("  -I <fname>: Load INI file\n");
  381.         printf("  -B <num>  : Set beat peak level to <num>\n");
  382.         printf("  -b <num>  : Set # of beats to <num>   (-ve to disable)\n");
  383.  
  384.         printf("  -X,-x     : Debug mode (no sound source needed)\n\n");
  385.         exit(1);
  386.      }
  387.  
  388.  
  389.     printf("\n\n");
  390.     if( mouse_inst = mouse_init() )
  391.         printf("Mouse found.\n");
  392.     else
  393.         printf("Mouse not found.\n");
  394.  
  395.  
  396.     printf("Initializing Soundcard...\n");
  397.  
  398.     if (!debug_mode) {
  399.         switch (sound_card) {
  400.             case OLDSB:
  401.                 printf("looking for a SB (old code)\n\n");
  402.                 printf("**WARNING** this old code may not be stable\n");
  403.                 printf("Especially the CD-ROM screen & code...\n\n");
  404.                 if (SDK_init()) {
  405.                     printf("No Sound source, exiting\n");
  406.                     exit(1);
  407.                 }
  408.                 break;
  409.             case GUS:
  410.                 printf("looking for a GUS\n");
  411.  
  412.                 if (GUS_init()) {
  413.                     printf("No Sound source, exiting\n");
  414.                     exit(1);
  415.                 }
  416.                 break;
  417.             case SBPRO:
  418.                 printf("looking for a Soundblaster\n");
  419.                 if (SB_init()) {
  420.                     printf("No Sound source, exiting\n");
  421.                     exit(1);
  422.                 }
  423.  
  424.                 break;
  425.  
  426.             default: // Try Everything!!
  427.                 printf("Scanning for audio cards\n");
  428.                 printf("looking for a GUS\n");
  429.                 if (!GUS_init()) {
  430.                     break;
  431.                 }
  432.  
  433.                 printf("looking for a Soundblaster\n");
  434.                 if (!SB_init()) {
  435.                     printf("(If this is a PAS, you might have to rerun with -P)\n");
  436.                     break;
  437.                 }
  438.  
  439.                 printf("looking for an old Soundblaster\n");
  440.                 if (!SDK_init()) {
  441.                     printf("**WARNING** this old code may not be stable\n");
  442.                     printf("Especially the CD-ROM screen & code...\n\n");
  443.                     break;
  444.                 }
  445.  
  446.                 printf("Unable to find a soundcard\n");
  447.                 exit(1);
  448.                 break;
  449.         }
  450.     }
  451.  
  452.  
  453.     if( in_vol > 0 ) {
  454.         in_vol = min(in_vol,255);
  455.  
  456.         switch( device ) {
  457.             case CDInput:        set_level(MIXcd        ,in_vol); break;
  458.             case LineInput:   set_level(MIXline      ,in_vol); break;
  459.             case MicInput:    set_level(MIXmicrophone,in_vol); break;
  460.         }
  461.     }
  462.  
  463.     if (stringfile[0]!=0) {
  464.         if ((fp=fopen(stringfile,"r"))!=NULL) {
  465.             i=0;
  466.             fgets(stringfile,254,fp);
  467.             while (!feof(fp) && i<19) {
  468.                 stringfile[strlen(stringfile)-1]=0;
  469.                 stringfile[20]=0;
  470.                 sprintf(stringtable[i],"%s",stringfile);
  471. //                printf("%s\n",stringtable[i]);
  472.                 i++;
  473.                 fgets(stringfile,254,fp);
  474.             }
  475.             fclose(fp);
  476.             
  477.         }
  478.     }
  479.     printf("Loading Palettes\n");
  480.  
  481.     if (use_internal_palettes) {
  482.         for (i=0; i<numluts; i++)
  483.             for (j=0; j<255; j++)
  484.                 LUTfiles[i][j]=LUTfiles[i][j]>>2;
  485.  
  486.         for( i=0,j=0;i < 64;i++ ) {
  487.                 LUTfiles[numluts][j++] = i;
  488.                 LUTfiles[numluts][j++] = 0;
  489.                 LUTfiles[numluts][j++] = 0;
  490.         }
  491.         for( i=0;i < 128;i++ ) {
  492.                 LUTfiles[numluts][j++] = 63;
  493.                 LUTfiles[numluts][j++] = 0;
  494.                 LUTfiles[numluts][j++] = i;
  495.         }
  496.         for( i=0;i < 64;i++ ) {
  497.                 LUTfiles[numluts][j++] = 63-i;
  498.                 LUTfiles[numluts][j++] = 0;
  499.                 LUTfiles[numluts][j++] = 63;
  500.         }
  501.  
  502.         i=numluts+1;
  503.     } else {
  504.         numluts=0;
  505.         i=0;
  506.     }
  507.     
  508.     if (use_external_palettes) {
  509.         if (!_dos_findfirst("*.map",_A_NORMAL,&dos_f))
  510.             do {
  511.                 printf("reading %s as palette %d\n",dos_f.name,i+1);
  512.                 if (LoadLuts(dos_f.name,LUTfiles[i])==0)
  513.                     i++;
  514.                 else
  515.                     printf("problem with palette file: %s\n",dos_f.name);
  516.             } while (!_dos_findnext(&dos_f) && i<MAXLUTS);
  517.         
  518.         numluts=i;
  519.     }
  520.  
  521.     if (numluts<=0) {
  522.         printf("No Palettes loaded, exiting\n");
  523.         exit(2);
  524.     }
  525.  
  526.     init_pete();
  527.     init_translate();
  528.  
  529.     for (temp=0; temp<BUFF_WIDTH; temp++) {
  530.         buff[BUFF_BOTTOM+1][temp]=0;
  531.         buff[BUFF_BOTTOM+2][temp]=0;
  532.     }
  533.  
  534.  
  535.     for (j=0; j<NUMTABLES; j++) {
  536.         for (i=0; i<256; i++)
  537.             switch(j) {
  538.                 default:
  539.                 case 0:
  540.                     table[j][i]=abs(128-i)*2;
  541.                     break;
  542.                 case 1:
  543.                     table[j][i]=255-abs(128-i)*2;
  544.                     break;
  545.                 case 2:
  546.                     table[j][i]=i;
  547.                     break;
  548.                 case 3:
  549.                     table[j][i]=255-i;
  550.                     break;
  551.                 case 4:
  552.                     table[j][i]=abs(128-i)+127;
  553.                     break;
  554.                 case 5:
  555.                     table[j][i]=255-abs(128-i)+127;
  556.                     break;
  557.                 case 6:
  558.                     table[j][i]=rand()%256;
  559.                     break;
  560.                 case 7:
  561.                     if (abs(128-i)<64)
  562.                         table[j][i]=255;
  563.                     else
  564.                         table[j][i]=(abs(128-i)*4);
  565.                     break;
  566.                 case 8:
  567.                     table[j][i]=abs(128-i);
  568.                     break;
  569.                 case 9:
  570.                     table[j][i]=255-abs(128-i);
  571.                     break;
  572.             }
  573.     }
  574.  
  575.     lastmode=0;
  576.     current=0;
  577.     finished=0;
  578.  
  579.  
  580.  
  581.  
  582.     if (inifile[0]!=0) {
  583.         if ((fp=fopen(inifile,"r"))!=NULL) {
  584.             printf("Using %s for all settings\n",inifile);
  585. //            useinifile=1;
  586.             iniline=0;
  587.             fgets(string,254,fp);
  588.             while (iniline<MAX_INI_SETTINGS && !feof(fp)) {
  589.                 if (string[0]!=';' && string[2]!=0) {
  590.                     n=sscanf(string,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %u %d %d",
  591.                         &personalINI[iniline].min_time,
  592.                         &personalINI[iniline].rand_time,
  593.                         &personalINI[iniline].quiet_change,
  594.                         &personalINI[iniline].curtable,
  595.                         &personalINI[iniline].curflame,
  596.                         &personalINI[iniline].curdisplay,
  597.                         &personalINI[iniline].locked,
  598.                         &personalINI[iniline].minnoise,
  599.                         &personalINI[iniline].massageStyle,
  600.                         &personalINI[iniline].allow_fft,
  601.                         &personalINI[iniline].use_fft,
  602.                         &personalINI[iniline].peaklevel,
  603.                         &personalINI[iniline].peakframes,
  604.                         &personalINI[iniline].newwave,
  605.                         &personalINI[iniline].usewave,
  606.                         &personalINI[iniline].extra,
  607.                         &personalINI[iniline].curpal,
  608.                         &personalINI[iniline].sample_rate,
  609.                         &personalINI[iniline].sample_stereo,
  610.                         &personalINI[iniline].translate);
  611.  
  612.  
  613.                     if (rand_time<=0)
  614.                         rand_time=1;
  615.  
  616.                     if (n==20)
  617.                         iniline++;
  618.                     else
  619.                         printf("Bad INI line: %s",string);
  620.                 } else {
  621.                     printf("%s",string);
  622.                 }
  623.                 fgets(string,254,fp);
  624.             }
  625.             fclose(fp);
  626.             printf("%d configurations read\n",iniline);
  627.         } else {
  628.             useinifile=0;
  629.             printf("Cannot find INI file: %s\n",inifile);
  630.         }
  631.     }
  632.  
  633.     if (pause) {
  634.         printf("\nPausing, press any key to continue:");
  635.         getch();
  636.     } else {
  637.         delay(2000);
  638.     }
  639.  
  640.     regset.x.ax = 0x0013;    
  641.     int86(0x10, ®set, ®set);
  642.  
  643.     curpal=curpal%numluts;
  644.     curflame=change_flame(curflame);
  645.     change_wave(newwave);
  646.     curdisplay=0;
  647.  
  648.     FillLUTBuffer(curpal);
  649.  
  650.  
  651.     if (iniline>0) {
  652.         n=rand()%iniline;
  653.  
  654.         min_time=personalINI[n].min_time;
  655.         rand_time=personalINI[n].rand_time;
  656.         quiet_change=personalINI[n].quiet_change;
  657.         curtable=personalINI[n].curtable;
  658.         curflame=personalINI[n].curflame;
  659.         curdisplay=personalINI[n].curdisplay;
  660.         locked=personalINI[n].locked;
  661.         minnoise=personalINI[n].minnoise;
  662.         massageStyle=personalINI[n].massageStyle;
  663.         allow_fft=personalINI[n].allow_fft;
  664.         use_fft=personalINI[n].use_fft;
  665.         peaklevel=personalINI[n].peaklevel;
  666.         peakframes=personalINI[n].peakframes;
  667.         newwave=personalINI[n].newwave;
  668.         usewave=personalINI[n].usewave;
  669. //    extra=personalINI[n].extra;
  670.         curpal=personalINI[n].curpal;
  671.         sample_rate=personalINI[n].sample_rate;
  672.         sample_stereo=personalINI[n].sample_stereo;
  673.         translate=personalINI[n].translate;
  674.  
  675.         FillLUTBuffer(curpal%numluts);
  676.         curflame=change_flame(curflame);
  677.         change_wave(usewave);
  678.  
  679.     }
  680.  
  681.  
  682.     while (!finished) {
  683.  
  684.         switch (current) {
  685.             case 0:
  686.                 time_to_change=0;
  687.  
  688.                 for (temp=0; temp<BUFF_WIDTH; temp++) {
  689.                     buff[BUFF_BOTTOM+1][temp]=0;
  690.                     buff[BUFF_BOTTOM+2][temp]=0;
  691.                 }
  692.                 flame_cro();
  693.                 doit=0;
  694.                 break;
  695.             default:
  696.             case 1:  // Change everything (In Theory)
  697.  
  698.                 current=0;
  699.                 if (iniline<=0)  // No INI lines, always random
  700.                     n=0;   
  701.                 else if (!useinifile) {
  702.                     n=rand()%2;  // Default INI file, plus random
  703.                 } else {
  704.                     n=1;         // Personal INI file, no random
  705.                 }
  706.  
  707.                 if (!n) {
  708.                     time_to_change=0;
  709.                     curtable=rand()%NUMTABLES;
  710.                     FillLUTBuffer(rand()%numluts);
  711.                     curflame=change_flame(rand());
  712.                     change_wave(rand()%NUMWAVES);
  713.                     curdisplay=rand()%NUMDISPLAYS;
  714.                     if ((rand()%5)==0)
  715.                         translate = !translate;
  716.                     if ((rand()%5)==0)
  717.                         use_fft=!use_fft;
  718.                     doit=1;
  719.                 } else {
  720.                     doit=1;
  721.                     time_to_change=0;
  722.  
  723.                     n=rand()%iniline;
  724.  
  725.                     min_time=personalINI[n].min_time;
  726.                     rand_time=personalINI[n].rand_time;
  727.                     quiet_change=personalINI[n].quiet_change;
  728.                     curtable=personalINI[n].curtable;
  729.                     curflame=personalINI[n].curflame;
  730.                     curdisplay=personalINI[n].curdisplay;
  731.                     locked=personalINI[n].locked;
  732.                     minnoise=personalINI[n].minnoise;
  733.                     massageStyle=personalINI[n].massageStyle;
  734.                     allow_fft=personalINI[n].allow_fft;
  735.                     use_fft=personalINI[n].use_fft;
  736.                     peaklevel=personalINI[n].peaklevel;
  737.                     peakframes=personalINI[n].peakframes;
  738.                     newwave=personalINI[n].newwave;
  739.                     usewave=personalINI[n].usewave;
  740. //                    extra=personalINI[n].extra;
  741.                     curpal=personalINI[n].curpal;
  742.                     sample_rate=personalINI[n].sample_rate;
  743.                     sample_stereo=personalINI[n].sample_stereo;
  744.                     translate=personalINI[n].translate;
  745.  
  746.                     FillLUTBuffer(curpal%numluts);
  747.                     curflame=change_flame(curflame);
  748.                     change_wave(usewave);
  749.  
  750.                 }
  751.                 break;
  752.         }
  753.  
  754.         switch (z_keypress()) {
  755.             case Z_NOKEY:
  756.                 if (doit) {
  757.                     current=0;
  758.                     doit=0;
  759.                 } else {
  760.                     if (!locked) {
  761.                         current=rand()%NUMCOMBINATIONS+1;
  762.                     }
  763.                 }
  764.                 break;
  765.             case Z_ESC:
  766.                 finished=1;
  767.                 break;
  768.  
  769.             case Z_FFT:
  770.                 use_fft=!use_fft;
  771. //                allow_fft= !allow_fft;
  772.                 break;
  773.             case Z_NOISE_DN:
  774.                 minnoise=max(minnoise-1,0);
  775.                 break;
  776.  
  777.             case Z_SAMPLE_DN:
  778.                 sample_rate -= 1000;
  779.                 break;
  780.  
  781.             case Z_NOISE_UP:
  782.                 minnoise=min(minnoise+1,127);
  783.                 break;
  784.  
  785.             case Z_SAMPLE_UP:
  786.                 sample_rate += 1000;
  787.                 break;
  788.  
  789.             case Z_STEREO:
  790.                 sample_stereo = !sample_stereo;
  791.                 sample_rate = 0;
  792.                 break;
  793.  
  794.             case Z_CD:
  795. //                if (sound_card != OLDSB)
  796.                     cd_player();
  797.                 break;
  798.  
  799.             case Z_WAVE:
  800.                 next_wave();
  801.                 break;
  802.  
  803.             case Z_FLAME:
  804.                 curflame++;
  805.                 curflame=change_flame(curflame);
  806.                 break;
  807.  
  808.             case Z_PALETTE:
  809.                 FillLUTBuffer((curpal+1)%numluts);
  810.                 break;
  811.  
  812.             case Z_TABLE:
  813.                 curtable=(curtable+1)%NUMTABLES;
  814.                 break;
  815.  
  816.             case Z_DISPLAY:
  817.                 curdisplay=(curdisplay+1)%NUMDISPLAYS;
  818.                 break;
  819.  
  820.             case Z_OTHERKEY:
  821.             case Z_SPACE:
  822.                 current=rand()%NUMCOMBINATIONS+1;
  823.                 break;
  824.  
  825.             case Z_NEWWAVE:
  826.                 change_wave(newwave);
  827.                 break;
  828.  
  829.             case Z_MASSAGE:
  830.                 massageStyle = (massageStyle+1)%NUMMASSAGES;
  831.                 break;
  832.  
  833.             case Z_LOCK:
  834.                 locked=(!locked);
  835.                 if (!locked)
  836.                     current=rand()%NUMCOMBINATIONS+1;
  837.                 break;
  838.  
  839.             case Z_HELP:
  840.                 regset.x.ax = 0x0003;    /* AL = 3 selects 80x25 text mode */
  841.                 int86(0x10, ®set, ®set);
  842.  
  843.                 help_screen();
  844.  
  845.                 gotoxy(0,24);
  846.                 printf("Current: wave=%d, flame=%d, sample rate=%ld, %s, minnoise=%d",usewave,curflame,(long)sample_rate,sample_stereo ? "stereo" : "mono",minnoise);
  847.  
  848.                 while (z_keypress()==Z_NOKEY)
  849.                     ;
  850.  
  851.                 regset.x.ax = 0x0013;
  852.                 int86(0x10, ®set, ®set);
  853.  
  854.                 FillLUTBuffer((curpal)%numluts);
  855.                 break;
  856.  
  857.             case Z_TRANSLATE:
  858.                 translate = !translate;
  859.                 break;
  860.  
  861.             case Z_ADDINI:
  862.  
  863.                 if ((fp=fopen(inifile,"a"))!=NULL) {
  864.                     regset.x.ax = 0x0003;    /* AL = 3 selects 80x25 text mode */
  865.                     int86(0x10, ®set, ®set);
  866.  
  867.                     printf("Adding to %s\n",inifile);
  868.                     printf("Adding to current settings list\n");
  869.                     if (iniline>=MAX_INI_SETTINGS) {
  870.                         printf("Too many settings.. cannot save, sorry :-)\n");
  871.                     } else {
  872.  
  873.                         personalINI[iniline].min_time=min_time;
  874.                         personalINI[iniline].rand_time=rand_time;
  875.                         personalINI[iniline].quiet_change=quiet_change;
  876.                         personalINI[iniline].curtable=curtable;
  877.                         personalINI[iniline].curflame=curflame;
  878.                         personalINI[iniline].curdisplay=curdisplay;
  879.                         personalINI[iniline].locked=locked;
  880.                         personalINI[iniline].minnoise=minnoise;
  881.                         personalINI[iniline].massageStyle=massageStyle;
  882.                         personalINI[iniline].allow_fft=allow_fft;
  883.                         personalINI[iniline].use_fft=use_fft;
  884.                         personalINI[iniline].peaklevel=peaklevel;
  885.                         personalINI[iniline].peakframes=peakframes;
  886.                         personalINI[iniline].newwave=newwave;
  887.                         personalINI[iniline].usewave=usewave;
  888.                         personalINI[iniline].extra=0;
  889.                         personalINI[iniline].curpal=curpal;
  890.                         personalINI[iniline].sample_rate=sample_rate;
  891.                         personalINI[iniline].sample_stereo=sample_stereo;
  892.                         personalINI[iniline].translate=translate;
  893.  
  894.                         fprintf(fp,"; Auto Added from Cthugha\n");
  895.                         fprintf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %u %d %d\n",
  896.                         personalINI[iniline].min_time,
  897.                         personalINI[iniline].rand_time,
  898.                         personalINI[iniline].quiet_change,
  899.                         personalINI[iniline].curtable,
  900.                         personalINI[iniline].curflame,
  901.                         personalINI[iniline].curdisplay,
  902.                         personalINI[iniline].locked,
  903.                         personalINI[iniline].minnoise,
  904.                         personalINI[iniline].massageStyle,
  905.                         personalINI[iniline].allow_fft,
  906.                         personalINI[iniline].use_fft,
  907.                         personalINI[iniline].peaklevel,
  908.                         personalINI[iniline].peakframes,
  909.                         personalINI[iniline].newwave,
  910.                         personalINI[iniline].usewave,
  911.                         personalINI[iniline].extra,
  912.                         personalINI[iniline].curpal,
  913.                         personalINI[iniline].sample_rate,
  914.                         personalINI[iniline].sample_stereo,
  915.                         personalINI[iniline].translate);
  916.                         fclose(fp);
  917.  
  918.                         iniline++;
  919.                     }
  920.  
  921.                 } else
  922.                     printf("Cannot open INI file: %s\n",inifile);
  923.  
  924.             printf("Press a key to continue:");
  925.  
  926.             while (z_keypress()==Z_NOKEY)
  927.                 ;
  928.  
  929.  
  930.             regset.x.ax = 0x0013;
  931.             int86(0x10, ®set, ®set);
  932.  
  933.             FillLUTBuffer((curpal)%numluts);
  934.  
  935.             default:
  936.                 break;
  937.         }
  938.     }
  939.  
  940.  
  941.  
  942.     regset.x.ax = 0x0003;    /* AL = 3 selects 80x25 text mode */
  943.     int86(0x10, ®set, ®set);
  944.  
  945.     if (!debug_mode)
  946.         close_audio();
  947.  
  948.     show_credits();
  949.     getch();
  950.     quit_screen();
  951.  
  952.     return 0;
  953. }
  954.  
  955. #if 0
  956. void FillLUTBuffer(int pal)
  957. {
  958.     /* fills the buffer with 8 bit values */
  959.  
  960.         curpal=pal%numluts;
  961.  
  962.         memcpy(LUTbuffer,LUTfiles[curpal],768);
  963.  
  964.  
  965.     _asm {
  966.             push di;
  967.             push si;
  968.             push ds;
  969.             push ss;
  970.             push sp;
  971.             push bp;
  972.  
  973.     mov     dx,0x03da
  974. WaitVS:
  975.     in      al,dx
  976.     test    al,0x08
  977.     jz      WaitVS  ;vertical sync is active high (1 = active)
  978.  
  979.  
  980.             mov ax,1012h;
  981.             mov bx,0;
  982.             mov cx,100h;
  983.             mov dx,SEG LUTbuffer;
  984.             mov es,dx;
  985.             mov dx,OFFSET LUTbuffer;
  986.             int 10h;
  987.             pop bp;
  988.             pop sp;
  989.             pop ss;
  990.             pop ds;
  991.             pop si;
  992.             pop di;
  993.     }
  994. }
  995.  
  996. #else
  997. void FillLUTBuffer(int pal)
  998. {
  999.      /* fills the buffer with 8 bit values */
  1000.  
  1001.     int i;
  1002.  
  1003.     char *p;
  1004.  
  1005.     curpal=pal%numluts;
  1006.  
  1007.     memcpy(LUTbuffer,LUTfiles[curpal],768);
  1008.  
  1009.     outp(0x3c6,0xff);
  1010.  
  1011.     p = &LUTbuffer;
  1012.  
  1013.     for(i=0;i<256;i++)
  1014.     {
  1015.         outp(0x3c8,i);
  1016.         outp(0x3c9,(*p++));
  1017.         outp(0x3c9,(*p++));
  1018.         outp(0x3c9,(*p++));
  1019.     }
  1020.  
  1021. }
  1022. #endif
  1023.  
  1024. static int LoadLuts( char * fn, unsigned char *pal)
  1025. {
  1026.     FILE *f;
  1027.     int n;
  1028.     unsigned        r, g, b, index;
  1029.     char    line[160];
  1030.     char    temp[81];
  1031.  
  1032.     strcpy (temp,fn);
  1033.     if (strchr(temp,'.') == NULL) /* Did name have an extension? */
  1034.         strcat(temp,".map");  /* No? Then add .map */
  1035.  
  1036.     f = fopen( temp, "r" );
  1037.     if (f == NULL) {
  1038.         return 1;
  1039.     }
  1040.     for( index = 0; index < 256; index++ ) {
  1041.         if (fgets(line,100,f) == NULL)
  1042.             return 1;
  1043.         n=sscanf( line, "%u %u %u", &r, &g, &b );
  1044.         if (n<3)
  1045.             return 1;
  1046.         /** load global dac values **/
  1047.         pal[index*3]   = (r%256) >> 2;     /* maps default to 8 bits */
  1048.         pal[index*3+1] = (g%256) >> 2;     /* DAC wants 6 bits */
  1049.         pal[index*3+2] = (b%256) >> 2;
  1050.     }
  1051.     fclose( f );
  1052.     if (index==0) {
  1053.         pal[0]=pal[1]=pal[2]=0;
  1054.         index++;
  1055.     }
  1056.  
  1057.     while (index < 256)  { /* zap unset entries */
  1058.         pal[index*3] =  pal[(index-1)*3];
  1059.         pal[index*3+1] = pal[(index-1)*3+1];
  1060.         pal[index*3+2] = pal[(index-1)*3+2]; 
  1061.         ++index;
  1062.     }
  1063.     return 0;
  1064. }
  1065.  
  1066.  
  1067.